<template>
	<view>
		<view class="box">
			
		</view>
		<view class="text">
			<image src="../../static/kc.png" class="boxlogo" mode="widthFix"></image>
			<text style="text-align: center;">{{name}}</text></br>
			<text>申请获取得以下权限</text></br>
			<text>获得你的个人信息（昵称，头像等）</text>
		</view>
		<button class="login" type="primary" open-type="getUserInfo" @getuserinfo="getuserinfo">登录</button>
	</view>
</template>

<script>
	import http from '../../utils/http.js';
	export default {
		data() {
			return {
				name:"",
				logourl:""
			}
		},
		mounted:function(){
		uni.getUserInfo({
			success: (res) => {
				console.log(res);
				this.name = res.userInfo.nickname;
				this.logourl = res.userInfo.avatarUrl;
			},fail: () => {
		                        console.log("未授权");
		                    }
		})	
		},
		methods: {
			getuserinfo:function(res){
				console.log(res);
				if(!res.detail.iv){
					uni.showToast({
						title:"您取消了授权",
						icon:"none",						
					})
					return false;
				}else{
					uni.showLoading({ title: '正在登陆...', mask: true });
					uni.login({
						
						provider:"weixin",
						success:function(loginRes){
							console.log(loginRes); 
							//获取sessionkey
							uni.request({
								url:'https://www.camwork.site/login.ashx',
								data:{
									code:loginRes.code
								},
								header:{
									 'content-type': 'application/json'
								},
								method:'GET',
								success:function(res1){
									uni.hideLoading();
									console.log(res1);
									 uni.setStorageSync('suid',res1.data.openid);
									 if(res1.statusCode==200){
											var pages = getCurrentPages();
											var prevPage = pages[pages.length - 2]; //上一个页面
											//直接调用上一个页面的setData()方法，把数据存到上一个页面中去
											// 上一个页面最后设置userdata
											prevPage.setData({
											userdata: {
											'item':{
												name:res.detail.userInfo.nickName,
												city:res.detail.userInfo.city,
												imageurl:res.detail.userInfo.avatarUrl,
												openid:res1.data.openid
											}
											}
											})
											uni.navigateBack({//返回
											delta: 1
											})
									 }
									 
								},
								
							})
							
						}
					})
				}
			
			}
		}
	}
</script>

<style>
.box{
	margin-top: 200upx;
	
}
.boxlogo{
	margin: 0 auto;
	width: 200upx;
	height: 200upx;
	
}
.text{
	
	line-height: 80upx;
	margin:  0 auto;
	text-align: center;
	font-size: 35upx;
	
}
.login{
	width: 90%;
	margin: 0 auto;
	height: 100upx;
	text-align: center;
	line-height: 100upx;
	margin-top: 50upx;
}
</style>
